library(marginaleffects)

# Start log file for script -----------------------------------------------
TeachingDemos::txtStart("./log files/figureA27_28_ideowhite_prep.txt")

# Load Data ---------------------------------------------------------
# individual data
load("./data/impdat1920.Race.WeekWeight.rdata")
load("./data/impdat20.Race.WeekWeight.rdata")

impdat20.WeekWeight <- fastDummies::dummy_cols(impdat20.WeekWeight, select_columns = "week")
# Equalizing weeks with Nationscape
impdat20.WeekWeight$week2 <- as.numeric(impdat20.WeekWeight$week) + 1
impdat20.WeekWeight$week2[which(impdat20.WeekWeight$week2 == 53)] <-  52
impdat20.WeekWeight$week2 <- as.character(impdat20.WeekWeight$week2)
impdat20.WeekWeight$week2[which(nchar(impdat20.WeekWeight$week2) == 1)] <- paste0("0", impdat20.WeekWeight$week2[which(nchar(impdat20.WeekWeight$week2) == 1)])
impdat20.WeekWeight <- fastDummies::dummy_cols(impdat20.WeekWeight, select_columns = "week2")

impdat20.WeekWeight$week2_post <- "Pre-GF"
for(i in 22:52){
  impdat20.WeekWeight$week2_post[which(impdat20.WeekWeight[, paste0("week2_", i)] == 1)] <- paste("Week", i)
}

vars <- c("year", "week", "date_mdy", "ideo3_lab", "age_cat4", "edu_cat4", "race5", "sex",
          "census_region", "broughtwebsite2", "WeekWeights", "t_wb")
impdat19_sub <- subset(impdat1920.WeekWeight, select = vars)
impdat20_sub <- subset(impdat20.WeekWeight, select = vars)

impdat_comb <- rbind(impdat19_sub, impdat20_sub)


# Models ------------------------------------------------------------------
m_week1920_ideo <- lm(t_wb ~ 
                        as.factor(week)*year*ideo3_lab +
                        age_cat4 + edu_cat4 + sex +
                        census_region + broughtwebsite2,
                      data = impdat_comb, weights = WeekWeights,
                      subset = date_mdy != "2020-05-25" & race5 == "White")
summary(m_week1920_ideo)


# full pre-post
m1_ideo <- lm(t_wb ~ post_gf*ideo3_lab +
                age_cat4 + edu_cat4 + sex + 
                census_region + broughtwebsite2,
              data = impdat20.WeekWeight, weights = WeekWeights, subset = race5 == "White")
summary(m1_ideo)


# Weekly after
m1_pre_weeklypost_ideo <- lm(D_biep.White_Good_all ~ week2_post*ideo3_lab  +
                               age_cat4 + edu_cat4 + sex + 
                               census_region + broughtwebsite2,
                             data = impdat20.WeekWeight, weights = WeekWeights, subset = race5 == "White")
summary(m1_pre_weeklypost_ideo)


# Prediction Data Sets ------------------------------------------------
weeks <- names(table(m_week1920_ideo$model$`as.factor(week)`))

pdat_con <- data.frame(week = weeks,
                       year = c(rep(2019, length(weeks)), rep(2020, length(weeks))),
                       ideo3_lab = "conservative",
                       age_cat4 = "18-29",
                       edu_cat4 = "Post-Grad",
                       sex = "f",
                       census_region = "South",
                       broughtwebsite2 = "Assignment school/work")

pdat_mod <- data.frame(week = weeks,
                       year = c(rep(2019, length(weeks)), rep(2020, length(weeks))),
                       ideo3_lab = "moderate",
                       age_cat4 = "18-29",
                       edu_cat4 = "Post-Grad",
                       sex = "f",
                       census_region = "South",
                       broughtwebsite2 = "Assignment school/work")

pdat_lib <- data.frame(week = weeks,
                       year = c(rep(2019, length(weeks)), rep(2020, length(weeks))),
                       ideo3_lab = "liberal",
                       age_cat4 = "18-29",
                       edu_cat4 = "Post-Grad",
                       sex = "f",
                       census_region = "South",
                       broughtwebsite2 = "Assignment school/work")



# Plot --------------------------------------------------------------------
preds_con <- predict(m_week1920_ideo, pdat_con, se.fit = T)
preds_mod <- predict(m_week1920_ideo, pdat_mod, se.fit = T)
preds_lib <- predict(m_week1920_ideo, pdat_lib, se.fit = T)

p_dat <- data.frame(year = c(rep("2019", length(weeks)), rep("2020", length(weeks))),
                    week = weeks,
                    pred = c(preds_con$fit, preds_mod$fit, preds_lib$fit),
                    se = c(preds_con$se.fit, preds_mod$se.fit, preds_lib$se.fit),
                    ideo = c(rep("Conservative", length(weeks)*2), 
                             rep("Moderate", length(weeks)*2),
                             rep("Liberal", length(weeks)*2)))
p_dat$ideo <- factor(p_dat$ideo,
                     levels = c("Conservative", "Moderate", "Liberal"))
p_dat$week <- as.Date(paste(2020, p_dat$week, 1, sep="-"), "%Y-%U-%u")
p_dat$week[c(1,54,107,160, 213, 266)] <- "2020-01-01" # from 00 weeks


ggplot(p_dat, aes(x = week, y = pred, group = year)) +
  geom_point(size = 2, aes(shape = year, color = year),
             position = position_dodge(width = .5)) +
  geom_linerange(aes(ymin = pred - 1.65*se,
                     ymax = pred + 1.65*se,
                     color = year),
                 position = position_dodge(width = .5),
                 lwd = .75) +
  geom_smooth(data = subset(p_dat, week < "2020-5-25"), 
              span = .8,
              aes(x = week, y = pred, color = year), se = FALSE) +
  geom_smooth(data = subset(p_dat, week >= "2020-5-25"), 
              span = .8,
              aes(x = week, y = pred, color = year), se = FALSE) +
  theme_bw() +
  facet_grid(ideo~., scales = "free_y") +
  scale_color_manual(values = c("grey", "black"), name = "") +
  scale_shape_manual(values = c(1, 16), name = "") +
  labs(y = "Predicted D-Scores",
       x = "") +
  scale_y_continuous(labels = numform::ff_num(zero = 0, digits = 2)) +
  theme(legend.position = "bottom",
        plot.title = element_text(size = 20),
        axis.title = element_text(size = 14),
        axis.text = element_text(size = 12),
        legend.text = element_text(size = 14),
        strip.text = element_text(size = 20),
        strip.background = element_blank())


ideoW_1920_therm <- ggplot(p_dat, aes(x = week, y = pred, group = year)) +
  geom_point(size = 2, aes(shape = year, color = year),
             position = position_dodge(width = .5)) +
  geom_linerange(aes(ymin = pred - 1.39*se,
                     ymax = pred + 1.39*se,
                     color = year),
                 position = position_dodge(width = .5),
                 lwd = .75) +
  geom_smooth(data = subset(p_dat, week < "2020-5-25"), 
              span = .8,
              aes(x = week, y = pred, color = year), se = FALSE) +
  geom_smooth(data = subset(p_dat, week >= "2020-5-25"), 
              span = .8,
              aes(x = week, y = pred, color = year), se = FALSE) +
  theme_bw() +
  facet_grid(ideo~., scales = "free_y") +
  scale_color_manual(values = c("grey", "black"), name = "") +
  scale_shape_manual(values = c(1, 16), name = "") +
  labs(y = "Predicted White vs. Black Favorability",
       x = "") +
  scale_x_date(breaks = unique(p_dat$week)[c(1, 6, 10, 15, 19, 23, 28, 32, 37, 41, 45, 50)],
               labels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun",
                          "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"),
               minor_breaks = NULL) + 
  scale_y_continuous(labels = numform::ff_num(zero = 0, digits = 2)) +
  theme(legend.position = "bottom",
        plot.title = element_text(size = 20),
        axis.title = element_text(size = 14),
        axis.text = element_text(size = 12),
        legend.text = element_text(size = 14),
        strip.text = element_text(size = 20),
        strip.background = element_blank())

p_dat_wIdeo_therm <- p_dat

# Pre-Post Regressions ----------------------------------------------------
# pre-post full
d <- plot_cme(m1_ideo, "post_gf", "ideo3_lab", draw = F)
prepost <- data.frame(ideo = c("White Liberals", "White Conservatives", "White Neutrals\n(Moderates)"),
                      coef = d$estimate,
                      se = d$std.error)


# pre weekly post
dd <- plot_cme(m1_pre_weeklypost_ideo, "week2_post", "ideo3_lab", draw = F)
dd_lib <- subset(dd, ideo3_lab == "liberal")
dd_con <- subset(dd, ideo3_lab == "conservative")
dd_mod <- subset(dd, ideo3_lab == "moderate")
weekly_outs <- data.frame(ideo = c(rep("White Liberals", nrow(dd_lib)),
                                   rep("White Conservatives", nrow(dd_con)),
                                   rep("White Neutrals\n(Moderates)", nrow(dd_mod))),
                          week = c(gsub(" - Pre-GF", "", dd_lib$contrast),
                                   gsub(" - Pre-GF", "", dd_con$contrast),
                                   gsub(" - Pre-GF", "", dd_mod$contrast)),
                          coef = c(dd_lib$estimate, dd_con$estimate, dd_mod$estimate),
                          se = c(dd_lib$std.error, dd_con$std.error, dd_mod$std.error))

# Combine to plot
prepost <- subset(prepost, select = c("ideo", "coef", "se"))
prepost$week <- "Post GF, May 26, 2020-Dec 31, 2020"

coef_out <- rbind(prepost, weekly_outs)

coef_out$ideo <- factor(coef_out$ideo,
                        levels = c("White Liberals", "White Neutrals\n(Moderates)", "White Conservatives"))
coef_out$var <- as.factor(coef_out$week)
coef_out$var <- factor(coef_out$var,
                       levels = rev(levels(coef_out$var)))
coef_out$indx <- c(1, 1, 1, rep(0, nrow(weekly_outs)))
coef_out$indx <- ifelse(coef_out$indx == 1, "Pre-Post", "Weekly Indicators")

coef_out_ideo <- coef_out

# End log file for script -------------------------------------------------
TeachingDemos::txtStop()